<?php

// +----------------------------------------------------------------------
// | Shop-Demo for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2022~2023 Anyon <zoujingli@qq.com>
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// | 会员免费 ( https://thinkadmin.top/vip-introduce )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\data\controller\fwg;

use app\data\model\DataFwgPhone;
use app\data\model\DataFwgPhone as DataNewsItem;
use app\data\model\DataUser;
use app\data\service\NewsService;
use think\admin\Controller;
use think\admin\extend\CodeExtend;
use think\admin\helper\QueryHelper;
use think\facade\Db;

/**
 * 服务员手机号码管理
 * Class Item
 * @package app\data\controller\news
 */
class Fwgphone extends Controller
{
    /**
     * 文章内容管理
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->type = $this->get['type'] ?? 'index';
        DataNewsItem::mQuery($this->get)->layTable(function ()
        {
            $this->title = '手机号管理';
        }, function (QueryHelper $query)
        {
            $query->like('phone,nickname')->dateBetween('create_at');
            $query->where(['status' => intval($this->type === 'index'), 'deleted' => 0]);
        });
    }

    /**
     * 列表数据处理
     * @param  array  $data
     */
    protected function _page_filter(array &$data) {}

    /**
     * 添加文章内容
     */
    public function add()
    {
        $this->title = '添加内容';
        DataNewsItem::mForm('form');
    }

    /**
     * 编辑文章内容
     */
    public function edit()
    {
        $this->title = '编辑内容';
        DataNewsItem::mForm('form');
    }


    /**
     * 表单数据处理
     * @param  array  $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _add_form_filter(array &$data)
    {

        if ($this->request->isGet()) {

        } else {


            Db::name("data_user")->where('phone', $data['phone'])->update(['sf' => 2]);


        }
    }


    /**
     * 表单数据处理
     * @param  array  $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _edit_form_filter(array &$data)
    {

        if ($this->request->isGet()) {

        } else {


            $phone = Db::name("data_fwg_phone")->where('id', $data['id'])->value('phone');

            Db::name("data_user")->where('phone', $phone)->update(['sf' => 1]);
            Db::name("data_user")->where('phone', $data['phone'])->update(['sf' => 2]);

        }
    }


    /**
     * 表单数据处理
     * @param  array  $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if (empty($data['code'])) {

        }
        if ($this->request->isGet()) {

        } else {

        }
    }

    /**
     * 表单结果处理
     * @param  boolean  $state
     */
    protected function _form_result(bool $state, $data)
    {
        if ($state) {
            $this->handleUser($data);
            $this->success('保存成功！', 'javascript:history.back()');
        }
    }

    /**
     * 修改文章状态
     */
    public function state()
    {
        DataNewsItem::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除文章内容
     */
    public function remove()
    {
        $id = input('id');

        $phone = Db::name('data_fwg_phone')->where('id', $id)->value('phone');
        Db::name("data_user")->where('phone', $phone)->update(['sf' => 1]);
        DataNewsItem::mDelete();


    }

    /**
     * 文章内容选择
     * @login true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function select()
    {
        $this->get['status'] = 1;
        $this->index();
    }

    protected function handleUser(array $data)
    {
        /**
         * @var DataUser $user
         */
        $user = DataUser::query()->where(['phone' => $data['phone']])->find();
        if (!$user) {
            DataUser::create([
                'phone'    => $data['phone'],
                'nickname' => $data['nickname'],
                'vip_name' => DataUser::$userType[DataUser::USER_TYPE_SERVICE_OFFICER],
                'password' => md5($data['password'] ?? '123456'),
                'sf'       => DataUser::USER_TYPE_SERVICE_OFFICER,
                'headimg'  => '/upload/tx.jpg',
            ]);
            return;
        }
        $user->setAttr('sf', DataUser::USER_TYPE_SERVICE_OFFICER);
        if ($data['password'] != $user->password) {
            $user->setAttr('password', md5($data['password']));
        }
        $user->save();
    }
}
